package com.alpaak.hellospringboot.utils.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/***
 * Result
 * 统一返回结果
 *
 * @author 2020/9/14-14:26 by Ht.L
 */

@ApiModel(value="Result", description="返回参数")
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "业务说明")
    private String msg;

    @ApiModelProperty(value = "错误信息")
    private String errMsg;

    public static Result<String> succeed(String msg) {
        return succeedWith(null, GlobalConstant.SUCCESS, msg);
    }

    public static <T> Result<T> succeedWith(T data) {
        return succeedWith(data, GlobalConstant.SUCCESS, "");
    }

    public static <T> Result<T> succeedWith(T data, String msg) {
        return new Result<>(data, GlobalConstant.SUCCESS, msg,"");
    }

    public static <T> Result<T> succeedWith(T data, Integer code, String msg) {
        return new Result<>(data, code, msg,"");
    }

    public static Result<String> failed(String errMsg) {
        return failed(errMsg, GlobalConstant.ERROR);
    }

    public static <T> Result<T> failed(String errMsg, Integer code) {
        return failedWith(errMsg, code, null);
    }

    public static <T> Result<T> failedWith(String errMsg, Integer code, T data) {
        return new Result<>(data, code, "",errMsg);
    }

    public static Result<String> business(String msg) {
        return business(msg, GlobalConstant.ERROR);
    }

    public static <T> Result<T> business(String msg, Integer code) {
        return businessWith(msg, code, null);
    }

    public static <T> Result<T> businessWith(String msg, Integer code, T data) {
        return new Result<>(data, code, msg,"");
    }

    public static Result<String> loginBusiness(String msg) {
        return business(msg, GlobalConstant.NO_LOGIN);
    }

    public Result() {
    }

    public Result(T data, Integer code, String msg, String errMsg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
        this.errMsg = errMsg;
    }
}

